package ClientLibrary.UI;

import ClientLibrary.Board;
import ClientLibrary.Network.NetLayer;
import ClientLibrary.Position;
import CommonLibrary.CheckersPosition;
import CommonLibrary.Commnads.EnemyMoveRequest;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JFrame;

public class MainForm {
	
	private JFrame mainFrame = new JFrame("Checkers");
	private Container mainCont = mainFrame.getContentPane();
	private final int sizeX = 8;
	private final int sizeY = 8;
    private int side;
    private NetLayer netLayer;
	
	private CheckersButton[][] buttons = new CheckersButton[sizeX][sizeY];

    public MainForm(int side){
        this.side = side;
        this.netLayer = Board.getBoard().getLayer();
        if(this.side == 0)
        {
            try
            {
                EnemyMoveRequest request = new EnemyMoveRequest();
                request.setGameID(Board.getBoard().getGameId());
                request.setPlayerID(Board.getBoard().getPlayerId());
                this.netLayer.sendCommand(request);
            }
            catch (Exception error)
            {
                System.out.print("\n Error in sending an EnemyMoveRequest command \n");
            }
        }
    }

	public void paintFrame() {
		mainCont.setFocusable(true);
		mainFrame.setSize(sizeX * 60, sizeY * 60);
		mainFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		mainCont.setLayout(new GridLayout(sizeY, sizeX));

		for (int y = 0; y < sizeY; y++) {
			for (int x = 0; x < sizeX; x++) {
				buttons[x][sizeY - y - 1] = new CheckersButton(this, x, sizeY - y - 1);
				mainCont.add(buttons[x][sizeY - y - 1]);
			}
		}

        //this.updateCheckers();

        MenuForm.getCurrentForm().close();
        mainFrame.setVisible(true);
	}

    public void updateCheckers() {
		for (int x = 0; x < sizeX; x++)
        {
			for (int y = 0; y < sizeY; y++)
            {
				int[][] defaultBoard = CheckersPosition.defaultPositions;
				buttons[x][y].set(defaultBoard[x][y]);
			}
        }
	}

    public void updateCheckers(Position[][] positions) {
        for (int x = 0; x < sizeX; x++)
        {
            for (int y = 0; y < sizeY; y++)
            {
                if(positions[x][y].getChecker() != null)
                {
                    buttons[x][y].set(positions[x][y].getChecker().getType());
                }
                else
                {
                    if(!buttons[x][y].isClicked())
                    {
                        buttons[x][y].set(0);
                    }
                }
            }
        }
    }

    public void setBackgroundForButtons(ArrayList<Position> positions)
    {
        for(Position pos: positions)
        {
            this.buttons[pos.getPoint().getX()][pos.getPoint().getY()].setBackground(Color.RED);
            this.buttons[pos.getPoint().getX()][pos.getPoint().getY()].setType(22);
        }
    }

}
